﻿#include "OperSubsystemPlate.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysAbsShell.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

namespace ModelOper
{
    bool OperSubsystemPlate::execGUI()
    {
        return false;
    }
    bool OperSubsystemPlate::execProfession()
    {
        return false;
    }
    void OperSubsystemPlate::editSubsystem(const int seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        QObject object;
        object.setObjectName("actionEditPlate");
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionEditPlate");
        if (!oper) return;
        oper->setArgs("DataID", seaID);
        oper->setEmitter(&object);
        oper->actionTriggered();
    }
    void OperSubsystemPlate::removeSubsystem(const int seaID)
    {
        //获取管理器
        if (!_seaMgr)return;
        //移除并更新列表
        _seaMgr->removeDataByID(seaID);
        ModelOper::updateBrowserTable();
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateSEASubSystems();
    }
    void OperSubsystemPlate::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsSpectraManager* specMgr = phys->getFITKAcousticsSpectraManager();
        if (specMgr == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* nctMgr = phys->getFITKAcousticsNCTManager();
        if (nctMgr == nullptr) return;
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        QList<QString> list;

        if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsPhysicsProp*> propList = propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPUniformPlat);
            propList += propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPGeneralLaminatePhysicalProperty);
            propList += propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPRibbedphysicalProperty);
            for (auto value : propList) {
                list += value->getDataObjectName();
            }
        }
        else if (attrIndex == 2 ) {
            QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Damping);
            for (auto value : specList) {
                list += value->getDataObjectName();
            }
        }
        else if (attrIndex == 7 || attrIndex == 8) {
            QList<Acoustics::FITKAcousticsAbsNCT*> nctList = nctMgr->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::TreatmentLayUp);
            nctList += nctMgr->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::UserDefTreatment);
            nctList += nctMgr->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::MulitipleNCT);
            list += "None";
            for (auto value : nctList) {
                list += value->getDataObjectName();
            }
        }
        dlg.setDefaultValueQString(list);
        if (dlg.exec() != QDialog::Accepted) return;

        QString str = dlg.getValueQString();//需要getText
        int ID = 0;
        if (attrIndex == 1)
            ID = propMgr->getDataByName(str)->getDataObjectID();
        else if (attrIndex == 2)
            ID = specMgr->getDataByName(str)->getDataObjectID();
        else if ((attrIndex == 7 || attrIndex == 8) && str != "None")
            ID = nctMgr->getDataByName(str)->getDataObjectID();
        else
            ID = -1;

        for (int id : seaID)
        {
            Acoustics::FITKAbstractSEASubSysShell* shell = dynamic_cast
                <Acoustics::FITKAbstractSEASubSysShell*>(_seaMgr->getDataByID(id));
            if (shell == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //板物理属性
                shell->setPropSection(ID); break;
            case 2: //频谱
                shell->setFlexureSpectraID(ID); break;
            case 7: //外声学包
                shell->setOutsideNCTID(ID); break;
            case 8: //内声学包
                shell->setInsideNCTID(ID); break;
            default: break;
            }
        }

        ModelOper::updateBrowserTable();
    }

}

